//
//  MainSubLiteratureViewModel.m
//  EnvironmentalProtection
//
//  Created by HCreater on 2018/11/19.
//  Copyright © 2018年 HCreater. All rights reserved.
//

#import "LiteratureViewModel.h"

#import "CateInfosData.h"

@interface LiteratureViewModel ()

@property (nonatomic, readwrite, assign) NSInteger tvmType;
@property (nonatomic, copy) NSString* cateName;

@property (nonatomic, strong) ProvinceListData* zoneData;
@property (nonatomic, copy) NSString* zone;

@end

@implementation LiteratureViewModel

- (void)initialize
{
    [super initialize];
    
    @weakify(self);
    
    _tvmType = 0;
    
    _listType = -1;
    
    _tabIndex = 0;
    
    [[[RACObserve(self, cateID) filter:^BOOL(id value) {
        return value!=nil;
    }] distinctUntilChanged] subscribeNext:^(id x) {
       
        @strongify(self);
        
        DataManager* dm = [DataManager sharedDataManager];
        
        NSInteger count = dm.libraryCateInfos.datas.count;
        for (NSInteger index=0; index<count; index++)
        {
            CateInfoData* curCateInfoData = dm.libraryCateInfos.datas[index];
            
            if ([curCateInfoData.cateID isEqualToString:x])
            {
                self.listType = index;
             
                self.cateName = curCateInfoData.cateName;
                
                break;
            }
        }
    }];
    
    RACSignal* zoneChangeSignal = [RACObserve(self, zoneData) distinctUntilChanged];
    [[zoneChangeSignal replay] subscribeNext:^(id x)
     {
         @strongify(self);
         
         ProvinceListData* curPListData = x;
         
         if (x == nil)
         {
             self.zone = @"全国";
         }
         else
         {
             self.zone = curPListData.name;
         }
     }];
    
    // 地区
    [[[zoneChangeSignal replay] takeUntil:[self rac_willDeallocSignal]] subscribeNext:^(id x)
     {
         @strongify(self);
         
         ProvinceListData* curPListData = x;
         
         for (id tvmID in self.tableVMs)
         {
             if (self.tvmType == 0)
             {
                 ZCFGV2TableViewModel* tvm = tvmID;
                 tvm.areaID = curPListData ? curPListData.areaID : @"0";
                 [tvm.requestDataCmd execute:@{}];
             }
             else
             {
                 DocumentV2TableViewModel* tvm = tvmID;
                 tvm.areaID = curPListData ? curPListData.areaID : @"0";
                 [tvm.requestDataCmd execute:@{}];
             }
         }
     }];
}

- (void)setListType:(NSInteger)listType
{
    _listType = listType;
    
    @weakify(self);
    
    DataManager* dm = [DataManager sharedDataManager];
    
    CateInfosData* cateInfosData = dm.libraryCateInfos;
    CateInfoData* curCateInfoData = cateInfosData.datas[listType];
    _cateInfoData = [curCateInfoData copy];
    
    if (curCateInfoData.cateList==nil || curCateInfoData.cateList.count == 0)
    {
        _tabViewHeight = 0;
        
        _cateInfoData.cateList = @[_cateInfoData];
    }
    else
    {
        _tabViewHeight = 54;
    }
    
    if ([self.cateInfoData.cateID isEqualToString:@"1"] ||
        [self.cateInfoData.cateID isEqualToString:@"30"])
    {
        _tvmType = 0;
    }
    else
    {
        _tvmType = 1;
    }
    
    NSInteger cateCount = _cateInfoData.cateList.count;
    NSMutableArray* newTableVMS = [NSMutableArray array];
    for (NSInteger index=0; index<cateCount; index++)
    {
        CateInfoData* curSubCateInfoData = _cateInfoData.cateList[index];
        NSString* cateID = curSubCateInfoData.cateID;
        
        if (_tvmType == 0)
        {
            ZCFGV2TableViewModel* tvm = [[ZCFGV2TableViewModel alloc] initWithParams:@{}];
            tvm.perPage = List_PageSize;
            tvm.lastPage = 1000;
            tvm.cellHeight = 112;
            /// 允许下拉刷新
            tvm.shouldPullDownToRefresh = YES;
            /// 允许上拉加载
            tvm.shouldPullUpToLoadMore = YES;
            tvm.tag = 10+index;
            tvm.cateID = cateID;
            
            newTableVMS[newTableVMS.count] = tvm;
        }
        else
        {
            DocumentV2TableViewModel* tvm = [[DocumentV2TableViewModel alloc] initWithParams:@{}];
            tvm.perPage = List_PageSize;
            tvm.lastPage = 1000;
            tvm.cellHeight = 96;
            /// 允许下拉刷新
            tvm.shouldPullDownToRefresh = YES;
            /// 允许上拉加载
            tvm.shouldPullUpToLoadMore = YES;
            tvm.tag = 10+index;
            tvm.cateID = cateID;
            tvm.fcateID = self.cateID;
            
            newTableVMS[newTableVMS.count] = tvm;
        }
    }
    _tableVMs = newTableVMS;
    
    [[[RACObserve(self, tabIndex) skip:1] distinctUntilChanged] subscribeNext:^(id x) {
        @strongify(self)
        
        //判断当前是否需要请求数据
        NSInteger curTabIndex = [x integerValue];
        
        if (self.tvmType == 0)
        {
            ZCFGV2TableViewModel* curTVM = self.tableVMs[curTabIndex];
            
            if (curTVM.dataSource.count == 0)
            {
                [curTVM.requestDataCmd execute:@{}];
            }
        }
        else
        {
            DocumentV2TableViewModel* curTVM = self.tableVMs[curTabIndex];
            
            if (curTVM.dataSource.count == 0)
            {
                [curTVM.requestDataCmd execute:@{}];
            }
        }
    }];
}

- (void)resetZoneData:(ProvinceListData*)zoneData
{
    self.zoneData = zoneData;
}

@end
